namespace Xmef.Serializer
{
    using System;
    using System.IO;
    using System.IO.IsolatedStorage;
    using Xmef.Interfaces;
    using Xmef.Library;

    [Serializable]
    public class XmlIsolatedStorageSerializer<T> : XmlFileStreamSerializer<T>
    {
        public override bool Attach(string key)
        {
            lock (((XmlIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    if (FileName == null)
                    {
                        string[] files = IsolatedStorageFile.GetUserStoreForApplication().GetFileNames(key);
                        if ((files != null) && (files.Length == 1))
                        {
                            return Attach(files[0]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorSync(ex);
                }
                return false;
            }
        }

        protected override bool Deserialize(IXmlTypeFormatter formatter)
        {
            lock (((XmlIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    if (FileName != null)
                    {
                        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                        using (Stream stream = new IsolatedStorageFileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.None, file))
                        {
                            return Deserialize(stream, formatter);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorSync(ex);
                }
                return false;
            }
        }

        public override bool Detach(string key)
        {
            lock (((XmlIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    if (FileName == null)
                    {
                        string[] files = IsolatedStorageFile.GetUserStoreForApplication().GetFileNames(key);
                        if ((files != null) && (files.Length == 1))
                        {
                            return Detach(files[0]);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorSync(ex);
                }
                return false;
            }
        }

        protected override bool Serialize(T obj, IXmlTypeFormatter formatter)
        {
            lock (((XmlIsolatedStorageSerializer<T>) this))
            {
                try
                {
                    if (FileName != null)
                    {
                        IsolatedStorageFile file = IsolatedStorageFile.GetUserStoreForApplication();
                        using (Stream stream = new IsolatedStorageFileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None, file))
                        {
                            return Serialize(obj, stream, formatter);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.WriteErrorSync(ex);
                }
                return false;
            }
        }
    }
}

